<?php

class AdminController extends PermisionController {

    public $layout = '//layouts/column1';

    public function init() {
        Yii::app()->theme = 'admincp';
    }

    public function actionAjax() {
        $act = $_GET['act'];
        if ($act == 'doValue') {
            $sortNameAll = $_POST['setValue'];
            if (count($sortNameAll) > 0) {
                foreach ($sortNameAll as $id => $sortName) {
                    $model = $this->loadModel($id);
                    $model->value = $sortName;
                    $model->save();
                }
            }
        }
    }

    /**
     * Manages all models.
     */
    public function actionIndex() {
        $this->pageTitle = 'Adminstrator: Cấu hình website';
        $model = new Setting('search');
        $model->unsetAttributes(); // clear any default values
        if (isset($_GET['Setting']))
            $model->attributes = $_GET['Setting'];

        $this->render('admin', array(
            'model' => $model,
        ));
    }

    public function actionContact() {
        $this->pageTitle = 'Adminstrator: Cập nhật giới thiệu';
        $model = Setting::model()->findByAttributes(array('key' => 'contact'));
        if (!empty($_POST['Setting'])) {
            $model->value = $_POST['Setting']['value'];
            $model->save();
        }
        $this->render('contact', array('model' => $model));
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer $id the ID of the model to be loaded
     * @return Category the loaded model
     * @throws CHttpException
     */
    public function loadModel($id) {
        $model = Setting::model()->findByPk($id);
        if ($model === NULL)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    /**
     * Performs the AJAX validation.
     * @param Category $model the model to be validated
     */
    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

}
